#!/usr/local/bin/python3
# -*- coding: UTF-8 -*-
"""
@author:apple
@file:01-设计一个有getMin功能的栈.py
@time:2021/07/07
"""


class Stack:
    ...

    def __init__(self):
        ...
        self.fist_stack = []
        self.last_stack = []

    def pop(self):
        self.check()
        val = self.fist_stack.pop()
        min_val = self.last_stack[-1]

        if val == min_val:
            self.last_stack.pop()
        return val

    def push(self, val):
        ...
        self.fist_stack.append(val)
        if len(self.last_stack) == 0:
            self.last_stack.append(val)
        else:
            min_val = self.last_stack[-1]
            # 注意这里为 大于，也就是说val <= min_val 就把 val 加入到 last_stack 栈中
            if min_val > val:
                self.last_stack.append(val)

    def getMin(self):
        ...
        self.check()
        return self.last_stack[-1]

    def check(self):
        if not self.fist_stack:
            raise Exception('stack is empty')


if __name__ == '__main__':
    pass
    stack = Stack()
    stack.push(1)
    stack.push(-4)
    print(stack.getMin())
    stack.push(2)
    stack.pop()
    stack.pop()
    print(stack.getMin())
    stack.push(-1)
    print(stack.getMin())
    stack.push(-2)
    print(stack.getMin())
